﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GOFconsole
{
    public class Chapter18_Memento
    {
        public void MementoTest()
        { 
            //模型
            Originator o = new Originator();
            o.State = "On";                 //初始状态为On
            o.Show();

            Caretaker c = new Caretaker();
            c.Memento = o.CreateMemento();  //创建备忘录，保存状态，由于有了封装，完全隐藏了Originator的实现细节

            o.State = "Off";                //状态改为Off
            o.Show();
            o.SetMemento(c.Memento);        //恢复初始状态
            o.Show();
            //DEMO(游戏存档)
            //打BOSS前的初始状态
            GameRole luka = new GameRole();
            luka.GetInitState();
            luka.ShowState();
            //保存进度
            RoleStateCaretaker sl = new RoleStateCaretaker();
            sl.Memento = luka.SaveState();
            //打BOSS扑街，状态清0
            luka.FightBoss();
            luka.ShowState();
            //读档，恢复初始状态
            luka.RecoveryState(sl.Memento);
            luka.ShowState();


        }
    }

    #region 模型
    class Originator    //发起人
    {
        private string state;
        public string State             //需要保存的属性
        {
            get { return state; }
            set { state = value; }
        }
        public Memento CreateMemento()  //创建备忘录，将当前需要保存的信息导入并实例化出一个Memento对象
        {
            return new Memento(state);
        }
        public void SetMemento(Memento memento)
        {
            state = memento.State;
        }
        public void Show()
        {
            Console.WriteLine("State=" + state);
        }
    }

    class Memento   //备忘录类
    {
        private string state;
        public Memento(string state)
        {
            this.state = state;
        }
        public string State
        {
            get { return state; }
        }
    }

    class Caretaker //管理者类
    {
        private Memento memento;

        public  Memento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
    }
    #endregion

    #region DEMO(游戏存档)
    class GameRole
    {
        private int hp;
        public int Hp
        {
            get { return hp; }
            set { hp = value; }
        }
        private int atk;
        public int Atk
        {
            get { return atk; }
            set { atk = value; }
        }
        private int def;
        public int Def
        {
            get { return def; }
            set { def = value; }
        }
        //显示当前状态
        public void ShowState()
        {
            Console.WriteLine("角色当前状态：");
            Console.WriteLine("体力：{0}",this.hp);
            Console.WriteLine("攻击：{0}", this.atk);
            Console.WriteLine("防御：{0}", this.def);
            Console.WriteLine("");
        }
        //获取初始状态
        public void GetInitState()
        {
            this.hp = 100;
            this.atk = 100;
            this.def = 100;
        }
        //BOSS战扑街
        public void FightBoss()
        {
            this.hp = 0;
            this.atk = 0;
            this.def = 0;
        }
        //**使用备忘录模式SL
        //保存角色状态
        public RoleStateMemento SaveState()
        {
            return new RoleStateMemento(hp, atk, def);
        }
        //恢复角色状态
        public void RecoveryState(RoleStateMemento memento)
        {
            this.hp = memento.Hp;
            this.atk = memento.Atk;
            this.def = memento.Def;
        }
    }
    //角色状态存储箱
    class RoleStateMemento
    {
        //保存HP,ATK,DEF三个状态
        private int hp;
        public int Hp
        {
            get { return hp; }
            set { hp = value; }
        }
        private int atk;
        public int Atk
        {
            get { return atk; }
            set { atk = value; }
        }
        private int def;
        public int Def
        {
            get { return def; }
            set { def = value; }
        }
        public RoleStateMemento(int hp, int atk, int def)
        {
            this.hp = hp;
            this.atk = atk;
            this.def = def;
        }
    }
    //角色状态管理者
    class RoleStateCaretaker
    {
        private RoleStateMemento memento;
        internal RoleStateMemento Memento
        {
            get { return memento; }
            set { memento = value; }
        }
        
    }

    #endregion

}
